﻿using BLL;
using models;
using Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace yuejilalala
{
    public partial class introduction : System.Web.UI.Page
    {
        ProductMarager Productmarager = new ProductMarager();//商品BLL
        ProductTypeMarager productTypeMarager = new ProductTypeMarager();//商品类型BLL
        RemarkManager remark = new RemarkManager();
       public static string dizhi = "";
        public Remark remark2 = new Remark();
       
        protected void Page_Load(object sender, EventArgs e)
        {
           dizhi = Session["dizhi"].ToString();
                string proid = Request.QueryString["proid"];
                hf_proid.Value = proid;//保存商品id

                if (Request.QueryString["proid"]==null)
                {
                    Response.Redirect("home.aspx");
                }
            if (!IsPostBack)
            {
                userDl();
                //获取传来的商品id
                //评论绑定数据
                this.Repeater1.DataSource = remark.Getpro2(int.Parse(proid));
                this.Repeater1.DataBind();
                //根据ID查商品
                List<Product> Prolist = Productmarager.SelProduct(proid);
                lb_spm.Text= Prolist[0].Proname ;//商品名
                lb_cuXiao.Text= Prolist[0].Price.ToString();//促销价格
                lb_yuanJia.Text = (Prolist[0].Price * 2).ToString();//原价
                lb_ljpj.Text = remark.remarkcount(int.Parse(proid)).Proid.ToString();//累积评价
                                                                                     //商品规格迭代显示
                lb_qbpl.Text= remark.remarkcount(int.Parse(proid)).Proid.ToString();
                List<Product> productlist = Productmarager.SelProduct(proid);
                //截取商品规格
                string ProSpeci = productlist[0].Speci;
                string[] ProSpecis= ProSpeci.Split('/');
                Rept_gg.DataSource = ProSpecis;
                Rept_gg.DataBind();
                if (productlist[0].Pic!= null)
                {
                    //截取图片显示出来（放大镜位置）
                    string Propic = productlist[0].Pic;
                    string[] Pics = Propic.Split('|');
                    string[] Picss = new string[] { Pics[0] };//放大镜为第一图片 所以将第一个图片放入新的数组中
                                                              //放大镜
                    Resp_fdj.DataSource = Picss;//将新的数组显示出来
                    Resp_fdj.DataBind();
                    //下面的图(备选栏)
                    string[] Picssa;
                    if (Pics.Count() <= 1)
                    {

                        Picssa = new string[] { Pics[0] };//备选栏数量小于3 所以将第一个图片放入新的数组中
                    }
                    else
                    {

                        Picssa = new string[] { Pics[0], Pics[1], Pics[2] };//备选栏数量3 所以将前三个图片放入新的数组中
                    }
                    Resp_tu.DataSource = Picssa;
                    Resp_tu.DataBind();
                    //商品详情
                    Rept_proxj.DataSource = Pics;
                    Rept_proxj.DataBind();
                }
                else
                {
                    Resp_tu.DataSource = Prolist[0].Thumbnail;
                    Resp_tu.DataBind();
                    //商品详情
                    Rept_proxj.DataSource = Prolist[0].Thumbnail;
                    Rept_proxj.DataBind();
                }
          
                //看了又看 同店铺的几个商品
                Rept_klyk.DataSource = Productmarager.SelProductByStoreId(productlist[0].Id.ToString());
                Rept_klyk.DataBind();

                //产品类型显示
                List<ProductType> productType = productTypeMarager.GetProductCount(int.Parse(productlist[0].Protype.ToString()));
                //详细信息显示
                lb_cplx.Text = productType[0].TypeName;//产品类型
                lb_ylcd.Text = productlist[0].Proylcd;//原料产地
                lb_cd.Text = productlist[0].Place;//产地
                lb_plb.Text = productlist[0].Burd;//配料表
                lb_cpgg.Text = productlist[0].Speci;//产品规格
                lb_bzq.Text = productlist[0].Expir;//保质期
                lb_cpbzh.Text = productlist[0].Norm;//产品标准号
                lb_scxkz.Text = productlist[0].Perms;//生产许可证编号
                lb_syff.Text = productlist[0].Way;//食用方法
            }
        }

        protected void Rept_klyk_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (e.CommandName=="jingru")
            {
                Response.Redirect("introduction.aspx?proid=" + e.CommandArgument);
            }
        }

        protected void bt_lkgm_Click(object sender, EventArgs e)
        {
            if (Session["sessid"] == null)
            {
                Response.Redirect("login.aspx");
            }
            //立刻购买
            dingdan();
        }
        //订单
        
        OrderManager orderManager = new OrderManager();
        PayManager payManager = new PayManager();
        public void dingdan()
        {
            string ddh = DateTime.Now.Year.ToString()+"" + DateTime.Now.Month.ToString()+"" + DateTime.Now.Day.ToString()+"" + DateTime.Now.Second.ToString() +""+ ((DateTime.Now.Year + DateTime.Now.Month + DateTime.Now.Day + DateTime.Now.Second) * 3).ToString();//简单的订单号生成
                                                                                                    //点击结算 生成订单 
            string id = Session["sessid"].ToString();//用户id
            Order orders = new Order();
            orders.Orders = ddh;//订单号
            orders.DateNow = DateTime.Now;//当前时间
            orders.UserID = int.Parse(id);//暂时固定用户
            orders.State = 1;//1为未支付
            orderManager.InsertOrders(orders);
            //将当前商品进行结算
            //实例化购买信息详情
            Pay pay = new Pay();
            //查询当前对应商品的内容
            List<Product> PList = Productmarager.SelProduct(hf_proid.Value);
            pay.Userid = int.Parse(id);//用户ID
            pay.Storeid = PList[0].Stroe;//店铺ID
            pay.Proid = PList[0].Id;//商品ID
            pay.Count = int.Parse(text_box.Text);//数量
            pay.Sum = PList[0].Price*int.Parse(text_box.Text);//单物品总价
            pay.Batch = orders.Orders.ToString();//订单号
            payManager.add(pay);
            Session["ddh"] = ddh;//将商品的订单号放入session中 
            //跳转到结算界面
            Response.Redirect("ShopPay.aspx");

        }
        CartMarager cartMarager = new CartMarager();
        //登陆在左上方显示用户名方法					
        public void userDl()
        {
            if (Session["sess"] != null)
            {
                //用户存在则显示出来 并且隐藏注册和登陆显示
                User user = Session["sess"] as User;
                hl_dlm.Text = "用户名:" + user.UName;
                hl_dlm.Visible = true;//显示登陆名控件
                hl_dl.Visible = false;//请登陆隐藏
                hl_zc.Visible = false;//注册隐藏
            }

        }
        protected void bt_jrgwc_Click(object sender, EventArgs e)
        {
            if (Session["sessid"]==null)
            {
                Response.Redirect("login.aspx");
            }
            //加入购物车
            //查询当前对应商品的内容
            List<Product> PList = Productmarager.SelProduct(hf_proid.Value);
            //点击结算 生成订单 
            string id = Session["sessid"].ToString();//用户id
                                                     //然后将物品放入购物车
            List<Product> PLists = Productmarager.SelProduct(hf_proid.Value);
            Cart cart = new Cart();
            cart.Count = int.Parse(text_box.Text);//数量
            cart.Userid =int.Parse(id);//用户id
            cart.Storeid = PLists[0].Stroe;//店铺id
            cart.Proid = PLists[0].Id;//商品id
            if (cartMarager.addCart(cart))
            {
                Response.Write("<script>alert('加入购物车成功！')</script>");
            }
           
        }
    }
}